package com.tpc.eb.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tpc.eb.job.BusinessQuartz;
import com.tpc.eb.utils.DateUtil;
import com.tpc.eb.utils.FileUtil;
import com.tpc.eb.utils.OutputUtil;
import lombok.extern.slf4j.Slf4j;
import org.ini4j.Profile;
import org.quartz.CronExpression;
import org.quartz.SchedulerException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@Slf4j
public class BusinessService {

    public void businessService() throws IOException, SchedulerException {
        BusinessQuartz businessQuartz = new BusinessQuartz();
        String message = "";
        Map<String,String> mapOMSConfig = FileUtil.getOMSConfig();
        String URL = mapOMSConfig.get("URL");
        String AppKey = mapOMSConfig.get("AppKey");
        String AppSecret = mapOMSConfig.get("AppSecret");
        String SignNo = mapOMSConfig.get("SignNo");
        String AppType = mapOMSConfig.get("AppType");
        String Name = mapOMSConfig.get("Name");
        Map<String,String> DBConfig = FileUtil.getDBConfig();
        String dbType = DBConfig.get("dbType");
        JSONObject jsonObjectParams = new JSONObject();
        Profile.Section section = FileUtil.getInterfaceList("BusinessType");
        Set<Map.Entry<String, String>> setList = section.entrySet();
        for (Map.Entry<String, String> mapList : setList) {
            String PULLSQL = "";


            String mapListValue = mapList.getValue();
            String mapListKey = mapList.getKey();
            JSONObject jsonObject = JSON.parseObject(mapListValue);
            String code = jsonObject.getString("code");
            String PLAN = jsonObject.getString("plan");
            jsonObjectParams.put("BusinessType",mapListKey);
            jsonObjectParams.put("Method",jsonObject.getString("interfaceType"));
            jsonObjectParams.put("node",jsonObject.getString("node"));
            jsonObjectParams.put("code",code);
            jsonObjectParams.put("PLAN",PLAN);
            if(dbType.equals("ORACLE")) {
                PULLSQL = "select * from (select * from EB_" + code + " where AppID='"+AppKey+"' order by updateTime) where rownum=1";
            }else{
                PULLSQL = "select top 1 * from EB_" + code + " where AppID='"+AppKey+"' order by updateTime";
            }
            jsonObjectParams.put("PULLSQL",PULLSQL);
            jsonObjectParams.put("URL",URL);
            jsonObjectParams.put("AppKey",AppKey);
            jsonObjectParams.put("AppSecret",AppSecret);
            jsonObjectParams.put("SignNo",SignNo);
            jsonObjectParams.put("AppType",AppType);
            jsonObjectParams.put("Name",Name);
            if(CronExpression.isValidExpression(PLAN)){
                message = DateUtil.getNowDate() + "---------" + mapListKey + "任务已启动---------";
                OutputUtil.printLog(message);
                businessQuartz.businessJobStart(jsonObjectParams.toJSONString());
            }else{
                message = DateUtil.getNowDate() + "----------" + mapListKey + "任务配置错误----------";
                OutputUtil.printLog(message);
            }
/*
            Profile.Section sectionBusiness = null;
            if(FileUtil.getInterfaceList(mapListKey)!=null){
                sectionBusiness = FileUtil.getInterfaceList(mapListKey);
                Set<Map.Entry<String, String>> setBusinessList = sectionBusiness.entrySet();
                Iterator<Map.Entry<String, String>> iterator = setBusinessList.iterator();
                Map<String, String> mapBusinessList = new HashMap<>();
                while (iterator.hasNext()) {
                    Map.Entry<String, String> mapBusiness = iterator.next();
                    if (mapBusiness.getKey().indexOf("PULLSQL") > -1) {
                        mapBusinessList.put(mapBusiness.getKey(), mapBusiness.getValue());
                    }else if(mapBusiness.getKey().indexOf("PLAN") > -1){
                        PLAN = mapBusiness.getValue();
                    }else if(mapBusiness.getKey().indexOf("BACKSQL") > -1){
                        BACKSQL = mapBusiness.getValue();
                    }
                }
//                for (int i = 0; i < mapBusinessList.size(); i++) {
//                    String key = "PULLSQL" + (i+1);
//                    String value = mapBusinessList.get(key) != null ? mapBusinessList.get(key) : "";
//                    if (value.length() > 0) {
//                        PULLSQL = PULLSQL + " " + value;
//                    }
//                }
//                if(PULLSQL.toLowerCase().indexOf("where")==-1){
//                    PULLSQL = PULLSQL + " where ";
//                }else{
//                    PULLSQL = PULLSQL + " and ";
//                }

                jsonObjectParams.put("BACKSQL",BACKSQL);



            }
*/
        }
    }
}
